package com.appkubes.fintech.admin.task.serivce.imple;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.appkubes.fintech.admin.application.bill.dao.RentBillHeaderDao;
import com.appkubes.fintech.admin.application.bill.dao.RentBillLinesDao;
import com.appkubes.fintech.admin.application.bill.entity.dto.RentBillHeaderDTO;
import com.appkubes.fintech.admin.application.bill.entity.dto.RentBillLinesDTO;
import com.appkubes.fintech.admin.application.bill.enums.RentBillEnums;
import com.appkubes.fintech.admin.task.serivce.IPenaltyInterestService;
import com.appkubes.fintech.core.po.FintechRentBillHeader;
import com.appkubes.fintech.core.po.FintechRentBillLines;
import com.appkubes.fintech.core.po.FintechRentBillLinesExample;
import com.appkubes.fintech.core.pojo.FintechRentBillHeaderMapper;
import com.appkubes.fintech.core.pojo.FintechRentBillLinesMapper;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Transactional
@Service("penaltyInterestServiceImpl")
public class PenaltyInterestServiceImpl implements IPenaltyInterestService {
    @Autowired
    private RentBillLinesDao linesMapper;
    @Autowired
    private RentBillHeaderDao headerMapper;

    /**
     * @description: 获取 逾期中或还款中账单明细
     * @author: xqc
     * @date: 2020/1/3 15:57
     * @params:
     * @return:
     */
    public List<FintechRentBillLines> queryPenalty() {
        FintechRentBillLinesExample example = new FintechRentBillLinesExample();
        List list = new ArrayList();
        list.add(RentBillEnums.还款中.getValue());
        list.add(RentBillEnums.已逾期.getValue());
        example.createCriteria().andStatusIn(list);
        example.setOrderByClause(" create_time desc");
        return linesMapper.selectByExample(example);
    }

    /**
     * @description: 获取 逾期中或还款中账单明细
     * @author: xqc
     * @date: 2020/1/3 15:57
     * @params:
     * @return:
     */
    public List<FintechRentBillLines> queryUnStart() {
        FintechRentBillLinesExample example = new FintechRentBillLinesExample();
        FintechRentBillLinesExample.Criteria criteria = example.createCriteria();
        criteria.andStatusEqualTo(RentBillEnums.未开始.getValue());
        criteria.andPeriodStartDateLessThan(new Date());
        example.setOrderByClause(" create_time desc");
        return linesMapper.selectByExample(example);
    }

    /**
     * @description: 开启 环境计划表
     * @author: xqc
     * @date: 2020/1/3 16:27
     * @params:
     * @return:
     */
    @Override
    public void startPlan() {
        List<FintechRentBillLines> linesList = queryUnStart();
        for (FintechRentBillLines line : linesList) {
        startLine(line);
        }
    }


    /**
     * @description: 罚息计算 每天计算一次
     * @author: xqc
     * @date: 2020/1/3 16:26
     * @params:
     * @return:
     */
    @Override
    public void penaltyInterest() {
        List<FintechRentBillLines> linesList = queryPenalty();
        for (FintechRentBillLines lines : linesList) {
            penaltyLine(lines);
        }

    }

    /**
     * @description: 开始还款中
     * @author: xqc
     * @date: 2020/1/3 16:30
     * @params:
     * @return:
     */
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void startLine(FintechRentBillLines lines) {
        if (DateUtil.between(lines.getPeriodStartDate(),new Date(), DateUnit.SECOND)<0) { // 没有开始直接返回
            return;
        }
        if(Objects.equals(lines.getStatus(),RentBillEnums.未开始)) {
            lines.setStatus(RentBillEnums.还款中.getValue());  // 更形状他
            lines.setUpdateTime(new Date());
        }
        linesMapper.updateByPrimaryKey(lines);
    }


    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void penaltyLine(FintechRentBillLines lines) {
        FintechRentBillHeader header = headerMapper.selectByPrimaryKey(lines.getBillKey());
        if(header==null||lines==null){
            return;
        }
        if (lines.getStatus().equals(RentBillEnums.还款中.getValue())) {
            if (DateUtils.addDays(lines.getPeriodEndDate(), 1).getTime() > System.currentTimeMillis()) { // 没有逾期直接返回
                return;
            } else {
                lines.setStatus(RentBillEnums.已逾期.getValue());  // 更形状他
                header.setStatus(RentBillEnums.已逾期.getValue());
            }
        }
        if(lines.getBillLineKey().equals("6dbaadc9dbef40f1b4b6788262afa136")){
            System.out.println("ddddddd");
        }
        Integer overDueDays = (int) DateUtil.between(lines.getPeriodEndDate(), new Date(), DateUnit.DAY);
        Integer addDays = overDueDays - lines.getOverdueDays(); // 增加天数
        if (addDays <= 0) {  // 逾期天数为零
            return;
        }
        BigDecimal overDueAmt = new BigDecimal(lines.getUnpayAmount()) // 新增罚息
                .multiply(lines.getFineRate()).multiply(new BigDecimal(addDays))
                .setScale(BigDecimal.ROUND_HALF_UP, 0);
        lines.setOverdueDays(lines.getOverdueDays() + addDays);
        lines.setUnpayFineAmount(lines.getUnpayFineAmount() + overDueAmt.intValue());
        lines.setPeriodFineAmount(lines.getPeriodFineAmount() + overDueAmt.intValue());

        if(header.getSurpFineAmount()==null){
            header.setSurpFineAmount(0);
        }
        header.setSurpFineAmount(header.getSurpFineAmount() + overDueAmt.intValue());
        header.setBillFine(header.getBillFine() + overDueAmt.intValue());
        if (Objects.equals(lines.getPeriod(), header.getUnpayPeriods())) {
            header.setUnpayFineAmount(header.getUnpayFineAmount() + overDueAmt.intValue());
        }
        header.setUpdateTime(new Date());
        lines.setUpdateTime(new Date());
        headerMapper.updateByPrimaryKey(header);
        linesMapper.updateByPrimaryKey(lines);
        return;
    }


}
